package com.cheyunkeji.er.fragment.auction;

import android.content.Intent;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.Constants;
import com.cheyun.netsalev3.MyApplication;
import com.cheyunkeji.er.activity.auction.HomeActivity;

import com.cheyunkeji.er.activity.auction.NotificationActivity;
import com.cheyunkeji.er.app.PushMsgManager;
import com.cheyunkeji.er.app.UserInfoManager;
import com.cheyunkeji.er.base.BaseFragment;
import com.cheyunkeji.er.bean.PushMsgBean;
import com.cheyunkeji.er.utils.ChannelInfoUtil;
import com.cheyunkeji.er.view.auction.NotifyItemView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Michael Chen
 */

public class NotifyFragment extends BaseFragment {

    @BindView(R.id.ntfi_xunjia)
    NotifyItemView ntfiXunjia;
    @BindView(R.id.ntfi_paimai)
    NotifyItemView ntfiPaimai;
    @BindView(R.id.ntfi_debiao)
    NotifyItemView ntfiDebiao;
    @BindView(R.id.ntfi_liubiao)
    NotifyItemView ntfiLiubiao;
    @BindView(R.id.ntfi_yuzhan)
    NotifyItemView ntfi_yuzhan;

    private Intent intent = null;
    private PushMsgManager msgManager;

    @Override
    protected void init() {

    }

    @Override
    protected void initView() {

        ntfiXunjia.setOnClickListener(this);
        ntfiPaimai.setOnClickListener(this);
        ntfiDebiao.setOnClickListener(this);
        ntfiLiubiao.setOnClickListener(this);
        ntfi_yuzhan.setOnClickListener(this);

    }


    private void showUnreadMsgCount() {
        dealWithPushMsg(null);
    }


    @Override
    protected void initData() {

    }

    @Override
    protected void doInflaterView(LayoutInflater inflater, ViewGroup container) {
        contentView = inflater.inflate(R.layout.er_frag_notify_layout, container, false);
        ButterKnife.bind(this, contentView);
    }

    @Override
    public void onClick(View v) {

        if (null == msgManager)
            msgManager = PushMsgManager.getInstance(this.getActivity().getApplicationContext(), ChannelInfoUtil.getCurrentChannelInfo().getChannel() + UserInfoManager.getInstance(this.getActivity().getApplicationContext()).getAccount());

        switch (v.getId()) {
            case R.id.ntfi_xunjia:
                intent = new Intent(getActivity(), NotificationActivity.class);
                intent.putExtra(Constants.NOTIFICATION_PAGE_TYPE, PushMsgManager.MSG_TYPE_XUNJIA);
                startActivity(intent);
                msgManager.clearNotifyMsgRecordCount(PushMsgManager.MSG_TYPE_XUNJIA);
                break;
            case R.id.ntfi_paimai:
                intent = new Intent(getActivity(), NotificationActivity.class);
                intent.putExtra(Constants.NOTIFICATION_PAGE_TYPE, PushMsgManager.MSG_TYPE_PAIMAI);
                startActivity(intent);
                msgManager.clearNotifyMsgRecordCount(PushMsgManager.MSG_TYPE_PAIMAI);
                break;
            case R.id.ntfi_debiao:
                intent = new Intent(getActivity(), NotificationActivity.class);
                intent.putExtra(Constants.NOTIFICATION_PAGE_TYPE, PushMsgManager.MSG_TYPE_ZHONGBIAO);
                startActivity(intent);
                msgManager.clearNotifyMsgRecordCount(PushMsgManager.MSG_TYPE_ZHONGBIAO);
                break;
            case R.id.ntfi_liubiao:
                intent = new Intent(getActivity(), NotificationActivity.class);
                intent.putExtra(Constants.NOTIFICATION_PAGE_TYPE, PushMsgManager.MSG_TYPE_LIUBIAO);
                startActivity(intent);
                msgManager.clearNotifyMsgRecordCount(PushMsgManager.MSG_TYPE_LIUBIAO);
                break;
            case R.id.ntfi_yuzhan:
                intent = new Intent(getActivity(), NotificationActivity.class);
                intent.putExtra(Constants.NOTIFICATION_PAGE_TYPE, PushMsgManager.MSG_TYPE_YUZHAN);
                startActivity(intent);
                msgManager.clearNotifyMsgRecordCount(PushMsgManager.MSG_TYPE_YUZHAN);
                break;
            default:
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        showUnreadMsgCount();
    }

    @Override
    protected void lazyLoad() {
        super.lazyLoad();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    public void dealWithPushMsg(PushMsgBean msgBean) {

        PushMsgManager pushMsgManager = PushMsgManager.getInstance(MyApplication.getInstance(), ChannelInfoUtil.getCurrentChannelInfo().getChannel() + UserInfoManager.getInstance(this.getActivity()).getAccount());

        if (null == msgBean) {

            refreshXunJiaMsg(pushMsgManager);
            refreshYuZhanMsg(pushMsgManager);
            refreshPaiMaiMsg(pushMsgManager);
            refreshZhongBiaoMsg(pushMsgManager);
            refreshLiuBiaoMsg(pushMsgManager);
            ((HomeActivity) getActivity()).refreshBottomMsgCount();
            return;
        }

        switch (msgBean.getNotice_type()) {

            case PushMsgManager.MSG_TYPE_XUNJIA:

                refreshXunJiaMsg(pushMsgManager);
                break;
            case PushMsgManager.MSG_TYPE_YUZHAN:
                refreshYuZhanMsg(pushMsgManager);
                break;
            case PushMsgManager.MSG_TYPE_PAIMAI:
                refreshPaiMaiMsg(pushMsgManager);
                break;
            case PushMsgManager.MSG_TYPE_ZHONGBIAO:
                refreshZhongBiaoMsg(pushMsgManager);
                break;

            case PushMsgManager.MSG_TYPE_LIUBIAO:
                refreshLiuBiaoMsg(pushMsgManager);
                break;

            default:
                break;
        }
        ((HomeActivity) getActivity()).refreshBottomMsgCount();
    }


    private void refreshLiuBiaoMsg(PushMsgManager pushMsgManager) {
        PushMsgBean bean4 = pushMsgManager.getLatestMsgEntity(PushMsgManager.MSG_TYPE_LIUBIAO);
        if (null != bean4) {
            ntfiLiubiao.showContent(bean4.getTitle(), bean4.getBody(), bean4.getDateline());
            ntfiLiubiao.setUnreadMsgCount(pushMsgManager.getMsgCount(PushMsgManager.MSG_TYPE_LIUBIAO));
        }
    }

    private void refreshZhongBiaoMsg(PushMsgManager pushMsgManager) {
        PushMsgBean bean3 = pushMsgManager.getLatestMsgEntity(PushMsgManager.MSG_TYPE_ZHONGBIAO);
        if (null != bean3) {
            ntfiDebiao.showContent(bean3.getTitle(), bean3.getBody(), bean3.getDateline());
            ntfiDebiao.setUnreadMsgCount(pushMsgManager.getMsgCount(PushMsgManager.MSG_TYPE_ZHONGBIAO));
        }
    }

    private void refreshPaiMaiMsg(PushMsgManager pushMsgManager) {
        PushMsgBean bean2 = pushMsgManager.getLatestMsgEntity(PushMsgManager.MSG_TYPE_PAIMAI);
        if (null != bean2) {
            ntfiPaimai.showContent(bean2.getTitle(), bean2.getBody(), bean2.getDateline());
            ntfiPaimai.setUnreadMsgCount(pushMsgManager.getMsgCount(PushMsgManager.MSG_TYPE_PAIMAI));
        }
    }

    private void refreshYuZhanMsg(PushMsgManager pushMsgManager) {
        PushMsgBean bean1 = pushMsgManager.getLatestMsgEntity(PushMsgManager.MSG_TYPE_YUZHAN);
        if (null != bean1) {
            ntfi_yuzhan.showContent(bean1.getTitle(), bean1.getBody(), bean1.getDateline());
            ntfi_yuzhan.setUnreadMsgCount(pushMsgManager.getMsgCount(PushMsgManager.MSG_TYPE_YUZHAN));
        }
    }

    private void refreshXunJiaMsg(PushMsgManager pushMsgManager) {
        PushMsgBean bean = pushMsgManager.getLatestMsgEntity(PushMsgManager.MSG_TYPE_XUNJIA);
        if (null != bean) {
            ntfiXunjia.showContent(bean.getTitle(), bean.getBody(), bean.getDateline());
            ntfiXunjia.setUnreadMsgCount(pushMsgManager.getMsgCount(PushMsgManager.MSG_TYPE_XUNJIA));
        }
    }

}
